package com.lvmama.pangolin.conf;

import java.util.concurrent.Executor;

import org.apache.log4j.Logger;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class CustomeAsyncConfig implements AsyncConfigurer{
	/**  
	* @Title：CustomeAsyncConfig.java
	* @Author:zhoutianqi
	* @Description:同步配置
	* @Date：2017年2月18日下午1:32:27  
	*/
	private Logger logger = Logger.getLogger(CustomeAsyncConfig.class);
	@Value("#{'${ThreadPoolTaskExecutor_CorePoolSize}'}")
	private String corePoolSize;
	@Value("#{'${ThreadPoolTaskExecutor_MaxPoolSize}'}")
	private String maxPoolSize;
	@Value("#{'${ThreadPoolTaskExecutor_QueueCapacity}'}")
	private String queueCapacity;
	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		//线程池维护线程的最少数量
		executor.setCorePoolSize(Integer.parseInt(corePoolSize));
		//线程池维护线程的最大数量
		executor.setMaxPoolSize(Integer.parseInt(maxPoolSize));
		//线程池所使用的缓冲队列
		executor.setQueueCapacity(Integer.parseInt(queueCapacity));
		executor.setThreadNamePrefix("MyExecutor-");
		executor.initialize();
		logger.info("ThreadPoolTaskExecutor初始化完毕！#corePoolSize="+corePoolSize+"#maxPoolSize="+maxPoolSize+"#queueCapacity="+queueCapacity);
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		logger.info("AsyncUncaughtExceptionHandler配置完毕");
		return new CustomeAsyncUncaughtExceptionHandler();
	}
}
